//
//  PubScrollView.m
//  SUZHOUBANK
//
//  Created by saberLily on 16/4/14.
//  Copyright © 2016年 RuiFeng-WGCC. All rights reserved.
//

#import "PubScrollView.h"
#import <AFNetworking/UIImageView+AFNetworking.h>
#import "DetailTopView.h"
#define SmallViewWidth 330

@interface PubScrollView()<UIGestureRecognizerDelegate>
@property (nonatomic,assign) ScrollStyle style; /**< 样式 */
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UITapGestureRecognizer *tapImageGesture; /**< 手势 */
@end
@implementation PubScrollView
- (instancetype)initWithFrame:(CGRect)frame ScrollStyle:(ScrollStyle)scrollStyle
{
    self = [super initWithFrame:frame];
    if (self) {
        self.style = scrollStyle;
        [self initializeDataSource];
        

    }
    return self;
}
#pragma mark - initializeDataSource 初始化数据源
-(void)initializeDataSource{
    
    _imageUrlArr = [@[] mutableCopy];
    
    _imageName = [@[] mutableCopy];
    
}

#pragma mark - initializeUserInterface 初始化用户界面
-(void)initializeUserInterface{
    [self addSubview:self.scrollView];
    
    if (self.style == ScrollStyleTitleAndImage) {
        [_imageUrlArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIView *image = [[UIView alloc] initWithFrame:CGRectMake(idx*(CGRectGetWidth(self.scrollView.bounds)/3), 5, SmallViewWidth+30, CGRectGetHeight(self.bounds)-10)];
            
            UIImageView *backView = [[UIImageView alloc] initWithFrame:image.bounds];
            backView.image = [UIImage imageNamed:@"新推产品_底图"];
            
            [image addSubview:backView];
            
//            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 55, 250, 160)];
            UIButton *imageView = [[UIButton alloc] initWithFrame:CGRectMake(50, 55, 250, 160)];
            
            
//            [imageView setImageWithURL:[NSURL URLWithString:_imageUrlArr[idx]]];
            [imageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_imageUrlArr[idx]]]] forState:UIControlStateNormal];
            imageView.tag = idx;
            
//            [imageView addGestureRecognizer:self.tapImageGesture];
            [imageView addTarget:self action:@selector(tapImageGesture:) forControlEvents:UIControlEventTouchUpInside];
            
            [image addSubview:imageView];
            //文字
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15,10, 330, 50)];
            label.text =  [NSString stringWithFormat:@"【%@】",_imageName[idx]];
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:30];
            [image addSubview:label];
            
            [self.scrollView addSubview:image];
            
        }];
    }else if(self.style == ScrollStyleImageAndTitle){
        
        [_imageUrlArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIView *image = [[UIView alloc] initWithFrame:CGRectMake(idx*(CGRectGetWidth(self.scrollView.bounds)/3), 5, SmallViewWidth+30, CGRectGetHeight(self.bounds)-10)];
            image.backgroundColor = [UIColor whiteColor];
            
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 350, 190)];
            
            [imageView setImageWithURL:[NSURL URLWithString:_imageUrlArr[idx]]];
            [image addSubview:imageView];
            imageView.userInteractionEnabled = YES;
//            imageView.pre
            
//            [imageView addGestureRecognizer:self.tapImageGesture];
             UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageWithImageView)];
            
            [imageView addGestureRecognizer:tap];
            
            //文字
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetHeight(imageView.bounds)+5, 330, 30)];
            label.text =  [NSString stringWithFormat:@"【%@】",_imageName[idx]];
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:20];
            [image addSubview:label];
            [self.scrollView addSubview:image];
            
        }];
        
    }else{
        [_imageUrlArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
//            UIButton
//            UIImageView
          
                UIView *image = [[UIView alloc] initWithFrame:CGRectMake(idx*440, 15, SmallViewWidth+100, CGRectGetHeight(self.bounds)+20)];
                
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 420, 250)];
                imageView.image = [UIImage imageNamed:_imageUrlArr[idx]];
                [image addSubview:imageView];
                [self.scrollView addSubview:image];
            self.scrollView.contentSize = CGSizeMake(440*_imageUrlArr.count, self.bounds.size.height-20);
            self.scrollView.backgroundColor = [UIColor clearColor];
        }];
    }
    
}

#pragma mark *** Events ***

-(void)tapImageGesture:(UIButton *)sender{
    DetailTopView *view = [[DetailTopView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    view.detailImageUrl = _imageUrlArr[sender.tag];
    view.detailTitle = _imageName[sender.tag];
    [view reloadAllData];
    [UIView animateWithDuration:0.5f animations:^{
        [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:view];

    }];
}
#pragma mark *** GusDele ***


-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return false;
}
#pragma mark *** getters ***
-(UIScrollView *)scrollView{
    if (!_scrollView) {
        
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height-20)];
        _scrollView.contentSize = CGSizeMake(self.imageUrlArr.count* CGRectGetWidth(self.bounds)/3, self.bounds.size.height-20);
        _scrollView.contentOffset = CGPointMake(0, 0);
        _scrollView.bounces = false;
        _scrollView.scrollEnabled = true;
        // 设置是否显示横向指示器
        _scrollView.showsHorizontalScrollIndicator = YES;
        // 设置是否显示纵向指示器
        _scrollView.showsVerticalScrollIndicator = YES;
        _scrollView.backgroundColor = [UIColor whiteColor];
//        [_scrollView addGestureRecognizer:self.tapImageGesture];
        
    }
    return _scrollView;
}

-(void)tapImageWithImageView{
     NSLog(@"%@",NSStringFromSelector(_cmd));
}
-(UITapGestureRecognizer *)tapImageGesture{
    if (!_tapImageGesture) {
        _tapImageGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageWithImageView)];
//        _tapImageGesture.cancelsTouchesInView = NO;
//        _tapImageGesture.delegate = self;
   
    }
    return _tapImageGesture;
}
@end
